//
//  XZLCustomSelectionView.m
//  XZLife
//
//  Created by 梁尚嘉 on 16/1/4.
//  Copyright © 2016年 Sami-industry. All rights reserved.
//

#import "XZLCustomSelectionView.h"

#define kXZLContentWidth  (SM_SCREEN_WIDTH - SM_SIZE(20*2))

@interface XZLCustomSelectionView()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UIView *containView;
@property (nonatomic, strong) UIView *backgroundView;
@end
@implementation XZLCustomSelectionView

- (instancetype)init
{
    if (self = [super init]) {
        [self configure];
    }
    return self;
}

- (void)configure
{
    self.backgroundColor = [SM_blackColor colorWithAlphaComponent:0];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hide:)];
    [self.backgroundView addGestureRecognizer:tap];
    self.backgroundView.userInteractionEnabled = YES;
    [self addSubview:self.backgroundView];
    [self addSubview:self.containView];
    [self.containView addSubview:self.titleLabel];
    [self.containView addSubview:self.tableView];
    
    [self makeConstraint];
}

- (void)makeConstraint
{
    [self.backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.mas_centerY);
        make.centerX.equalTo(self.mas_centerX);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(@0);
        make.height.equalTo(@(SM_SIZE(100)));
        make.width.equalTo(@(kXZLContentWidth));
    }];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.bottom.equalTo(@0);
        make.height.equalTo(@(3*SM_SIZE(90)));
    }];
}


- (void)showInView:(UIView *)view
{
    [view addSubview:self];
    self.containView.transform = CGAffineTransformMakeScale(.01, .01);
    [UIView animateWithDuration:.25 animations:^{
        self.backgroundColor = [SM_blackColor colorWithAlphaComponent:.7];
        self.containView.transform = CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
    }];
}

- (void)hide:(UITapGestureRecognizer *)sender
{
    [self hide];
}

- (void)hide
{
    [UIView animateWithDuration:.25 animations:^{
        self.backgroundColor = [SM_blackColor colorWithAlphaComponent:0];
        self.containView.transform = CGAffineTransformMakeScale(.01, .01);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)layoutSubviews
{
    [self setFrame:self.superview.bounds];
}

- (void)setDataArray:(NSArray *)dataArray
{
    _dataArray = dataArray;
    NSInteger numOfRow = MAX(1,MIN(5, self.dataArray.count));
    CGFloat height = numOfRow*SM_SIZE(90);
    if (self.dataArray.count > 5) {
        height = 5.6*SM_SIZE(90);
    }
    [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.top.equalTo(self.titleLabel.mas_bottom);
        make.bottom.equalTo(@0);
        make.height.equalTo(@(height));
    }];
}

#pragma mark - TableView Delegate && Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return MAX(self.dataArray.count,1);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XZLCustomSelectionViewTableCell"];
    cell.textLabel.text = !self.dataArray ? @"暂无选项": self.dataArray[indexPath.row];
    cell.textLabel.textColor = SM_blackColor;
    cell.textLabel.font = SM_CFont(32);
    cell.textLabel.highlightedTextColor = SM_lightBlueColor;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    !_onSelect ?: !_dataArray.count ?: _onSelect(indexPath.row);
    [self hide];
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        
        [cell setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}

#pragma mark - Propertys

- (UIView *)backgroundView
{
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]init];
    }
    return _backgroundView;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]init];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = SM_SIZE(90);
        _tableView.backgroundColor = SM_darkGrayColor;
        _tableView.separatorColor = HEXCOLOR(@"e7e7e7");
        [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"XZLCustomSelectionViewTableCell"];
    }
    return _tableView;
}

-(UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.backgroundColor = SM_thumeColor;
        _titleLabel.textColor = SM_whiteColor;
        _titleLabel.font = SM_CFont(36);
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.text = @"未定义标题";
    }
    return _titleLabel;
}

- (UIView *)containView
{
    if (!_containView) {
        _containView = [[UIView alloc]init];
        _containView.layer.cornerRadius = SM_SIZE(10);
        _containView.clipsToBounds = YES;
    }
    return _containView;
}


#pragma mark - Setters
- (void)setTitle:(NSString *)title
{
    _title = title;
    self.titleLabel.text = _title;
    
}

@end
